import {
  DbType,
  Condition,
  If,
  While,
  Break,
  Continue,
  XExpression,
  Return,
  Statement,
  Document,
  Scalar,
  ExpandScalar,
  Integer,
  DbTypeFromScalar,
  Literal,
  Variant,
  XRowsets,
  Field,
  Select,
  RowObject,
  TableVariantBuilder,
  TableVariantMember,
  XTableVariant,
  XObjectName,
  XTable,
  Func,
  Procedure,
  BuiltIn,
  Star,
  XRowset,
  CompatiblifyTuple,
  Execute,
  Assignable,
  Assignment,
  TableVariant,
  Declare,
  When,
  Case,
  WithSelect,
  XWithSelect,
  SelectAliasObject,
  With,
  CreateTableBuilder,
  AlterTableBuilder,
  AlterView,
  CreateIndex,
  CreateProcedureBuilder,
  AlterProcedureBuilder,
  CreateFunctionBuilder,
  AlterFunctionBuilder,
  DropIndex,
  Annotation,
  Insert,
  Update,
  Delete,
  Block,
  AlterDatabase,
  Parameter,
  ScalarFromDbType,
  Sequence,
  GroupCondition,
  GroupExpression,
  UnaryOperation,
  UNARY_OPERATION_OPERATOR,
  BinaryOperation,
  BINARY_OPERATION_OPERATOR,
  Expression,
  isBaseScalar,
  LOGIC_OPERATOR,
  UnaryLogicCondition,
  UnaryCompareCondition,
  UNARY_COMPARE_OPERATOR,
  BinaryCompareCondition,
  BINARY_COMPARE_OPERATOR,
  ExistsCondition,
  createTableVariant,
  Table,
  XTables,
  ColumnsOf,
  Raw,
  isDbType,
  Numeric,
  SelectAction,
  VirtualExpression,
  Float,
  Decimal,
  Time,
  Uuid,
  CreateDatabaseBuilder,
  CreateSequenceBuilder,
  CreateViewBuilder,
  Sort,
  ConditionBuilder,
  VirtualCondition,
  ScalarFuncInvoke,
  DropTableBuilder,
  // Use,
} from './sql';
import {
  CreateSchemaBuilder,
  DropDatabaseBuilder,
  DropFunctionBuilder,
  DropProcedureBuilder,
  DropSchemaBuilder,
  DropSequenceBuilder,
  DropViewBuilder,
} from './sql/statement';
import { VirtualOperation } from './virtual-operation';

export interface SqlBuilder extends VirtualOperation {
  readonly type: typeof DbType;

  // use(database: string): Use;

  if(condition: Condition): If;

  while(condition: Condition): While;

  break(): Break;

  continue(): Continue;

  return(value?: XExpression): Return;
  /**
   * 创建一个SQL文档
   * @param statements 文档代码
   */
  doc(statements: Statement[]): Document;
  doc(...statements: Statement[]): Document;

  group(condition: Condition): Condition;
  group<T extends Scalar>(expr: XExpression<T>): Expression<T>;

  /**
   * 负号运算符 -
   */
  neg(expr: XExpression<number>): Expression<ExpandScalar<number>>;

  /**
   * 字符串连接运算
   */
  concat(
    ...strs: [
      XExpression<string>,
      XExpression<string>,
      ...XExpression<string>[]
    ]
  ): Expression<string>;

  /**
   * 算术运算 +
   * @param left 左值
   * @param right 右值
   * @returns 返回算术运算表达式
   */
  add<T extends Numeric>(
    left: XExpression<T>,
    right: XExpression<T>
  ): Expression<T>;

  /**
   * 算术运算 -
   * @param left 左值
   * @param right 右值
   * @returns 返回算术运算表达式
   */
  sub<T extends Numeric>(
    left: XExpression<T>,
    right: XExpression<T>
  ): Expression<T>;

  /**
   * 算术运算 *
   * @param left 左值
   * @param right 右值
   * @returns 返回算术运算表达式
   */
  mul<T extends Numeric>(
    left: XExpression<T>,
    right: XExpression<T>
  ): Expression<T>;

  /**
   * 算术运算 /
   * @param left 左值
   * @param right 右值
   * @returns 返回算术运算表达式
   */
  div<T extends Numeric>(
    left: XExpression<T>,
    right: XExpression<T>
  ): Expression<T>;

  /**
   * 算术运算 %
   * @param left 左值
   * @param right 右值
   * @returns 返回算术运算表达式
   */
  mod<T extends Numeric>(
    left: XExpression<T>,
    right: XExpression<T>
  ): Expression<T>;
  /*
   * 位算术运算 ^
   * @param left 左值
   * @param right 右值
   * @returns 返回算术运算表达式
   */
  xor<T extends Integer>(
    left: XExpression<T>,
    right: XExpression<T>
  ): Expression<T>;

  /**
   * 位算术运算 << 仅支持整型
   * @param left 左值
   * @param right 右值
   * @returns 返回算术运算表达式
   */
  shl<T extends Integer>(
    left: XExpression<T>,
    right: XExpression<T>
  ): Expression<T>;

  /**
   * 位算术运算 >>
   * @param left 左值
   * @param right 右值
   * @returns 返回算术运算表达式
   */
  shr<T extends Integer>(
    left: XExpression<T>,
    right: XExpression<T>
  ): Expression<T>;

  literal<T extends Scalar>(
    value: T,
    dbType?: DbTypeFromScalar<T>
  ): Literal<ExpandScalar<T>>;

  var<T extends Scalar, N extends string = string>(
    name: N,
    type: DbTypeFromScalar<T>
  ): Variant<ExpandScalar<T>, N>;

  /**
   * 创建一个字段
   */
  field<T extends Scalar, N extends string>(
    name: N,
    rowset?: XRowsets
  ): Field<T, N>;

  /**
   * 将多个查询条件通过 AND 合并成一个大查询条件
   * @param conditions 查询条件列表
   * @returns 返回逻辑表达式
   */
  and(conditions: Condition[]): Condition;
  and(...conditions: [Condition, Condition, ...Condition[]]): Condition;
  /**
   * 位算术运算 &
   * @param left 左值
   * @param right 右值
   * @returns 返回算术运算表达式
   */
  and<T extends Integer>(
    left: XExpression<T>,
    right: XExpression<T>
  ): Expression<T>;

  /**
   * 将多个查询条件通过 OR 合并成一个
   * @param conditions 查询条件列表
   * @returns 返回逻辑表达式
   */
  or(conditions: Condition[]): Condition;
  or(...conditions: [Condition, Condition, ...Condition[]]): Condition;
  /**
   * 位算术运算 |
   * @param left 左值
   * @param right 右值
   * @returns 返回算术运算表达式
   */
  or<T extends Integer>(
    left: XExpression<T>,
    right: XExpression<T>
  ): Expression<T>;

  /**
   * Not 逻辑运算
   * @param condition
   */
  not(condition: Condition): Condition;
  /**
   * 位算术运算 ~
   * @param value 左值
   * @param right 右值
   * @returns 返回算术运算表达式
   */
  not<T extends Integer>(value: XExpression<T>): Expression<T>;

  /**
   * 比较运算 IS NULL
   * @returns 返回比较运算符
   * @param expr 表达式
   */
  isNull(expr: XExpression<Scalar>): Condition;

  /**
   * 比较运算 IS NOT NULL
   * @param expr 表达式
   * @returns 返回比较运算符
   */
  isNotNull(expr: XExpression<Scalar>): Condition;

  eq<T extends Scalar>(left: XExpression<T>, right: XExpression<T>): Condition;

  /**
   * 比较运算 <>
   * @param left 左值
   * @param right 右值
   * @returns 返回比较运算对比条件
   */
  neq<T extends Scalar>(left: XExpression<T>, right: XExpression<T>): Condition;

  lt<T extends Scalar>(left: XExpression<T>, right: XExpression<T>): Condition;

  /**
   * 比较运算 <=
   * @param left 左值
   * @param right 右值
   * @returns 返回比较运算对比条件
   */
  lte<T extends Scalar>(left: XExpression<T>, right: XExpression<T>): Condition;

  /**
   * 比较运算 >
   * @param left 左值
   * @param right 右值
   * @returns 返回比较运算对比条件
   */
  gt<T extends Scalar>(left: XExpression<T>, right: XExpression<T>): Condition;

  /**
   * 比较运算 >=
   * @param left 左值
   * @param right 右值
   * @returns 返回比较运算对比条件
   */
  gte<T extends Scalar>(left: XExpression<T>, right: XExpression<T>): Condition;

  /**
   * 比较运算 LIKE
   * @param left 左值
   * @param right 右值
   * @returns 返回比较运算对比条件
   */
  like(left: XExpression<string>, right: XExpression<string>): Condition;

  /**
   * 比较运算 NOT LIKE
   * @param left 左值
   * @param right 右值
   * @returns 返回比较运算对比条件
   */
  notLike(left: XExpression<string>, right: XExpression<string>): Condition;
  /**
   * 比较运算 IN
   * @param left 左值
   * @param values 要比较的值列表
   * @returns 返回比较运算对比条件
   */
  in<T extends Scalar>(left: XExpression<T>, select: Select<any>): Condition;
  in<T extends Scalar>(
    left: XExpression<T>,
    values: XExpression<T>[]
  ): Condition;
  in<T extends Scalar>(
    left: XExpression<T>,
    values: XExpression<T>[] | Select<any>
  ): Condition;

  /**
   * 比较运算 NOT IN
   * @param left 左值
   * @param values 要比较的值列表
   * @returns 返回比较运算对比条件
   */
  notIn<T extends Scalar>(
    left: XExpression<T>,
    values: XExpression<T>[]
  ): Condition;

  /**
   * 判断是否存在
   * @param select 查询语句
   */
  exists(select: Select<any>): Condition;

  table<T extends RowObject = any>(
    name: string,
    members:
      | ((builder: TableVariantBuilder) => TableVariantMember[])
      | TableVariantMember[]
  ): XTableVariant<T>;
  table<T extends RowObject = any>(
    name: XObjectName,
    builtIn?: boolean
  ): XTable<T>;

  /**
   * 声明一个函数
   */
  func(name: XObjectName, builtIn?: boolean): Func;

  /**
   * 创建一个可供调用的存储过程函数
   */
  proc<
    R extends Scalar = number,
    O extends RowObject[] = never,
    N extends string = string
  >(
    name: XObjectName<N>,
    builtIn?: boolean
  ): Procedure<R, O, N>;

  builtIn<N extends string>(name: N): BuiltIn<N>;

  star(): Star<any>;

  invokeAsTable<T extends RowObject = any>(
    func: XObjectName | Func<string>,
    args: (Star | BuiltIn | XExpression<Scalar>)[]
  ): XRowset<T>;

  invokeAsScalar<T extends Scalar = any>(
    func: XObjectName | Func<string>,
    args: (Star | BuiltIn | XExpression<Scalar>)[]
  ): ScalarFuncInvoke<T>;

  makeInvoke(
    type: 'table',
    name: XObjectName,
    builtIn?: boolean
  ): (...args: (Star | BuiltIn | XExpression | undefined)[]) => XRowset<any>;
  makeInvoke<
    R extends RowObject,
    A extends (Scalar | Star | BuiltIn | undefined)[] = []
  >(
    type: 'table',
    name: XObjectName,
    builtIn?: boolean
  ): (...args: CompatiblifyTuple<A>) => XRowset<R>;
  makeInvoke(
    type: 'scalar',
    name: XObjectName,
    builtIn?: boolean
  ): (...args: (Star | BuiltIn | XExpression | undefined)[]) => Expression<any>;
  makeInvoke<
    R extends Scalar,
    A extends (Scalar | Star | BuiltIn | undefined)[] = []
  >(
    type: 'scalar',
    name: XObjectName,
    builtIn?: boolean
  ): (...args: CompatiblifyTuple<A>) => Expression<R>;

  /**
   * 创建一个可供JS调用的存储过程
   */
  makeExec(
    name: XObjectName,
    builtIn?: boolean
  ): (
    ...args: (BuiltIn | Scalar | XExpression | undefined)[]
  ) => Execute<any, any>;
  makeExec<
    R extends Scalar = number,
    A extends (BuiltIn | Scalar | XExpression | undefined)[] = [],
    O extends RowObject[] = []
  >(
    name: XObjectName,
    builtIn?: boolean
  ): (...args: CompatiblifyTuple<A>) => Execute<R, O>;

  //********************** statement **************************//

  /**
   * 赋值语句
   * @param left 左值
   * @param right 右值
   */
  set<T extends Scalar = any>(
    left: Assignable<T>,
    right: XExpression<T>
  ): Assignment<T>;

  /**
   * 变量声明
   * @param declares 变量列表
   */
  declare(
    ...members:
      | (Variant<any> | TableVariant<any>)[]
      | [(Variant<any> | TableVariant<any>)[]]
  ): Declare;

  /**
   * WHEN 语句块
   * @param expr
   * @param value
   */
  when<T extends Scalar>(
    expr: XExpression<Scalar>,
    value: XExpression<T>
  ): When<T>;

  case<E extends Scalar>(expr?: XExpression<E>): Case<any, E>;

  /**
   * With语句
   */
  with(
    ...rowsets:
      | WithSelect[]
      | [WithSelect[]]
      | XWithSelect[]
      | [XWithSelect[]]
      | [SelectAliasObject]
  ): With;

  union<T extends RowObject = any>(...selects: Select<T>[]): Select<T>;

  unionAll<T extends RowObject = any>(...selects: Select<T>[]): Select<T>;

  readonly createTable: CreateTableBuilder;

  readonly alterTable: AlterTableBuilder;

  createView: CreateViewBuilder;

  alterView<T extends RowObject = any, N extends string = string>(
    name: XObjectName<N>
  ): AlterView<T, N>;

  createIndex(name: string): CreateIndex;

  readonly createProcedure: CreateProcedureBuilder;

  readonly alterProcedure: AlterProcedureBuilder;

  readonly createFunction: CreateFunctionBuilder;

  readonly alterFunction: AlterFunctionBuilder;

  readonly dropProcedure: DropProcedureBuilder;

  readonly dropTable: DropTableBuilder;

  readonly dropView: DropViewBuilder;

  readonly dropFunction: DropFunctionBuilder;

  dropIndex<N extends string>(table: XObjectName, name: N): DropIndex<N>;

  annotation(...text: string[]): Annotation;

  note(text: string): Annotation;

  /**
   * 插入至表,into的别名
   * @param table
   * @param fields
   */
  insert<T extends RowObject = any>(
    table: XTables<T>,
    fields?: ColumnsOf<T>[] | Field<Scalar, ColumnsOf<T>>[]
  ): Insert<T>;
  /**
   * 更新一个表格
   * @param table
   */
  update<T extends RowObject = any>(table: XTables<T>): Update<T>;

  /**
   * 删除一个表格
   * @param table 表格
   */
  delete<T extends RowObject = any>(table: XTables<T>): Delete<T>;

  readonly select: SelectAction;

  raw(sql: string): any;

  block(...args: Statement[] | [Statement[]]): Block;

  execute<R extends Scalar = any, O extends RowObject[] = []>(
    proc: XObjectName | Procedure<R, O, string>,
    params?: (BuiltIn | XExpression)[]
    // | Parameter<JsConstant, string>[] | InputObject
  ): Execute<R, O>;

  readonly createDatabase: CreateDatabaseBuilder;

  readonly createSchema: CreateSchemaBuilder;

  readonly dropSchema: DropSchemaBuilder;

  alterDatabase(name: string): AlterDatabase;

  readonly dropDatabase: DropDatabaseBuilder;

  readonly createSequence: CreateSequenceBuilder;

  readonly dropSequence: DropSequenceBuilder;

  //******************end statement*******************//

  //*************************** 参数声明 ***************************//
  /**
   * input 参数
   */
  input<T extends Scalar, N extends string>(
    name: N,
    value: T
  ): Parameter<ExpandScalar<T>, N>;
  input<T extends DbType, N extends string>(
    name: N,
    type: T,
    value?: ScalarFromDbType<T>
  ): Parameter<ExpandScalar<ScalarFromDbType<T>>, N>;

  /**
   * output参数
   */
  output<T extends DbType, N extends string>(
    name: N,
    type: T,
    value?: ScalarFromDbType<T>
  ): Parameter<ExpandScalar<ScalarFromDbType<T>>, N>;

  /**
   * 创建一个输入输出参数
   */
  inoutput<T extends DbType, N extends string>(
    name: N,
    type: T,
    value?: ScalarFromDbType<T>
  ): Parameter<ExpandScalar<ScalarFromDbType<T>>, N>;

  //***************************End 参数声明 ***************************//

  sequence<T extends Numeric>(name: XObjectName): Sequence<T>;
}

export class XSqlBuilder implements SqlBuilder {
  get type(): typeof DbType {
    return DbType;
  }

  // use(database: string): Use {
  //   return new Use(database);
  // },

  if(condition: Condition): If {
    return new If(condition);
  }

  while(condition: Condition): While {
    return new While(condition);
  }

  break(): Break {
    return new Break();
  }

  continue(): Continue {
    return new Continue();
  }

  return(value?: XExpression): Return {
    return new Return(value);
  }

  /**
   * 创建一个SQL文档
   * @param statements 文档代码
   */
  doc(...statements: Statement[] | [Statement[]]): Document {
    const lines = Array.isArray(statements[0])
      ? statements[0]
      : (statements as Statement[]);
    return new Document(lines);
  }

  group(value: Condition | XExpression): any {
    if (Condition.isCondition(value)) {
      return new GroupCondition(value);
    }
    return new GroupExpression(value as XExpression);
  }

  /**
   * 负号运算符 -
   */
  neg(expr: XExpression<number>): Expression<ExpandScalar<number>> {
    return new UnaryOperation(UNARY_OPERATION_OPERATOR.NEG, expr);
  }

  /**
   * 字符串连接运算
   */
  concat(
    ...strs: [
      XExpression<string>,
      XExpression<string>,
      ...XExpression<string>[]
    ]
  ): Expression<string> {
    let exp = strs[0];
    for (let i = 1; i < strs.length; i++) {
      exp = new BinaryOperation(BINARY_OPERATION_OPERATOR.CONCAT, exp, strs[i]);
    }
    return exp as Expression<string>;
  }

  /**
   * 算术运算 +
   * @param left 左值
   * @param right 右值
   * @returns 返回算术运算表达式
   */
  add<T extends Numeric>(
    left: XExpression<T>,
    right: XExpression<T>
  ): Expression<T> {
    return new BinaryOperation(BINARY_OPERATION_OPERATOR.ADD, left, right);
  }

  /**
   * 算术运算 -
   * @param left 左值
   * @param right 右值
   * @returns 返回算术运算表达式
   */
  sub<T extends Numeric>(
    left: XExpression<T>,
    right: XExpression<T>
  ): Expression<T> {
    return new BinaryOperation(BINARY_OPERATION_OPERATOR.SUB, left, right);
  }

  /**
   * 算术运算 *
   * @param left 左值
   * @param right 右值
   * @returns 返回算术运算表达式
   */
  mul<T extends Numeric>(
    left: XExpression<T>,
    right: XExpression<T>
  ): Expression<T> {
    return new BinaryOperation(BINARY_OPERATION_OPERATOR.MUL, left, right);
  }

  /**
   * 算术运算 /
   * @param left 左值
   * @param right 右值
   * @returns 返回算术运算表达式
   */
  div<T extends Numeric>(
    left: XExpression<T>,
    right: XExpression<T>
  ): Expression<T> {
    return new BinaryOperation(BINARY_OPERATION_OPERATOR.DIV, left, right);
  }

  /**
   * 算术运算 %
   * @param left 左值
   * @param right 右值
   * @returns 返回算术运算表达式
   */
  mod<T extends Numeric>(
    left: XExpression<T>,
    right: XExpression<T>
  ): Expression<T> {
    return new BinaryOperation(BINARY_OPERATION_OPERATOR.MOD, left, right);
  }
  /*
   * 位算术运算 ^
   * @param left 左值
   * @param right 右值
   * @returns 返回算术运算表达式
   */
  xor<T extends Integer>(
    left: XExpression<T>,
    right: XExpression<T>
  ): Expression<T> {
    return new BinaryOperation(BINARY_OPERATION_OPERATOR.XOR, left, right);
  }

  /**
   * 位算术运算 << 仅支持整型
   * @param left 左值
   * @param right 右值
   * @returns 返回算术运算表达式
   */
  shl<T extends Integer>(
    left: XExpression<T>,
    right: XExpression<T>
  ): Expression<T> {
    return new BinaryOperation(BINARY_OPERATION_OPERATOR.SHL, left, right);
  }

  /**
   * 位算术运算 >>
   * @param left 左值
   * @param right 右值
   * @returns 返回算术运算表达式
   */
  shr<T extends Integer>(
    left: XExpression<T>,
    right: XExpression<T>
  ): Expression<T> {
    return new BinaryOperation(BINARY_OPERATION_OPERATOR.SHR, left, right);
  }

  literal<T extends Scalar>(
    value: T,
    dbType?: DbTypeFromScalar<T>
  ): Literal<ExpandScalar<T>> {
    return new Literal(value as any, dbType);
  }

  var<T extends Scalar, N extends string = string>(
    name: N,
    type: DbTypeFromScalar<T>
  ): Variant<ExpandScalar<T>, N> {
    return new Variant(name, type) as any;
  }
  /**
   * 创建一个字段
   */
  field<T extends Scalar, N extends string>(
    name: N,
    rowset?: XRowsets
  ): Field<T, N> {
    return new Field(name, rowset);
  }

  and(
    ...args:
      | Condition[]
      | [Condition[]]
      | [XExpression<Integer>, XExpression<Integer>]
  ): any {
    if (Array.isArray(args[0])) {
      args = args[0];
    }

    if (Expression.isExpression(args[0]) || isBaseScalar(args[0])) {
      return new BinaryOperation(
        BINARY_OPERATION_OPERATOR.AND,
        args[0],
        args[1] as XExpression<Integer>
      );
    }

    return ConditionBuilder.and(args as Condition[]);
  }

  rownum(orderBy: Sort[], partitionBy?: Field): Expression<number> {
    return new VirtualExpression(this.rownum.name, [orderBy, partitionBy]);
  }

  or(
    ...args:
      | Condition[]
      | [Condition[]]
      | [XExpression<Integer>, XExpression<Integer>]
  ): any {
    if (Array.isArray(args[0])) {
      args = args[0];
    }
    if (Expression.isExpression(args[0]) || isBaseScalar(args[0])) {
      return new BinaryOperation(
        BINARY_OPERATION_OPERATOR.OR,
        args[0],
        args[1] as XExpression<Integer>
      );
    }
    return ConditionBuilder.or(args as Condition[]);
  }

  not(arg: Condition | XExpression<Integer>): any {
    if (Expression.isExpression(arg) || isBaseScalar(arg)) {
      return new UnaryOperation(UNARY_OPERATION_OPERATOR.NOT, arg);
    }
    return new UnaryLogicCondition(LOGIC_OPERATOR.NOT, arg);
  }

  /**
   * 比较运算 IS NULL
   * @returns 返回比较运算符
   * @param expr 表达式
   */
  isNull(expr: XExpression<Scalar>): Condition {
    return new UnaryCompareCondition(UNARY_COMPARE_OPERATOR.IS_NULL, expr);
  }
  /**
   * 比较运算 IS NOT NULL
   * @param expr 表达式
   * @returns 返回比较运算符
   */
  isNotNull(expr: XExpression<Scalar>): Condition {
    return new UnaryCompareCondition(UNARY_COMPARE_OPERATOR.IS_NOT_NULL, expr);
  }

  eq<T extends Scalar>(left: XExpression<T>, right: XExpression<T>): Condition {
    return new BinaryCompareCondition(BINARY_COMPARE_OPERATOR.EQ, left, right);
  }

  /**
   * 比较运算 <>
   * @param left 左值
   * @param right 右值
   * @returns 返回比较运算对比条件
   */
  neq<T extends Scalar>(
    left: XExpression<T>,
    right: XExpression<T>
  ): Condition {
    return new BinaryCompareCondition(BINARY_COMPARE_OPERATOR.NEQ, left, right);
  }

  lt<T extends Scalar>(left: XExpression<T>, right: XExpression<T>): Condition {
    return new BinaryCompareCondition(BINARY_COMPARE_OPERATOR.LT, left, right);
  }
  /**
   * 比较运算 <=
   * @param left 左值
   * @param right 右值
   * @returns 返回比较运算对比条件
   */
  lte<T extends Scalar>(
    left: XExpression<T>,
    right: XExpression<T>
  ): Condition {
    return new BinaryCompareCondition(BINARY_COMPARE_OPERATOR.LTE, left, right);
  }
  /**
   * 比较运算 >
   * @param left 左值
   * @param right 右值
   * @returns 返回比较运算对比条件
   */
  gt<T extends Scalar>(left: XExpression<T>, right: XExpression<T>): Condition {
    return new BinaryCompareCondition(BINARY_COMPARE_OPERATOR.GT, left, right);
  }
  /**
   * 比较运算 >=
   * @param left 左值
   * @param right 右值
   * @returns 返回比较运算对比条件
   */
  gte<T extends Scalar>(
    left: XExpression<T>,
    right: XExpression<T>
  ): Condition {
    return new BinaryCompareCondition(BINARY_COMPARE_OPERATOR.GTE, left, right);
  }
  /**
   * 比较运算 LIKE
   * @param left 左值
   * @param right 右值
   * @returns 返回比较运算对比条件
   */
  like(left: XExpression<string>, right: XExpression<string>): Condition {
    return new BinaryCompareCondition(
      BINARY_COMPARE_OPERATOR.LIKE,
      left,
      right
    );
  }
  /**
   * 比较运算 NOT LIKE
   * @param left 左值
   * @param right 右值
   * @returns 返回比较运算对比条件
   */
  notLike(left: XExpression<string>, right: XExpression<string>): Condition {
    return new BinaryCompareCondition(
      BINARY_COMPARE_OPERATOR.NOT_LIKE,
      left,
      right
    );
  }

  in<T extends Scalar>(
    left: XExpression<T>,
    values: XExpression<T>[] | Select<any>
  ): Condition {
    return new BinaryCompareCondition(
      BINARY_COMPARE_OPERATOR.IN,
      left,
      Select.isSelect(values)
        ? values.asValue()
        : values.map(v => Expression.ensureExpression(v))
    );
  }

  /**
   * 比较运算 NOT IN
   * @param left 左值
   * @param values 要比较的值列表
   * @returns 返回比较运算对比条件
   */
  notIn<T extends Scalar>(
    left: XExpression<T>,
    values: XExpression<T>[]
  ): Condition {
    return new BinaryCompareCondition(
      BINARY_COMPARE_OPERATOR.NOT_IN,
      left,
      values.map(v => Expression.ensureExpression(v))
    );
  }
  /**
   * 判断是否存在
   * @param select 查询语句
   */
  exists(select: Select<any>): Condition {
    return new ExistsCondition(select);
  }

  table(
    name: XObjectName,
    builtInOrMembers:
      | boolean
      | TableVariantMember[]
      | ((builder: TableVariantBuilder) => TableVariantMember[]) = false
  ): any {
    if (
      typeof builtInOrMembers === 'function' ||
      Array.isArray(builtInOrMembers)
    ) {
      return createTableVariant(
        name as string,
        builtInOrMembers as
          | ((builder: TableVariantBuilder) => TableVariantMember[])
          | TableVariantMember[]
      );
    }
    return Table.create(name, builtInOrMembers as boolean);
  }
  /**
   * 声明一个函数
   */
  func(name: XObjectName, builtIn = false): Func {
    return new Func(name, builtIn);
  }
  /**
   * 创建一个可供调用的存储过程函数
   */
  proc<
    R extends Scalar = number,
    O extends RowObject[] = never,
    N extends string = string
  >(name: XObjectName<N>, builtIn = false): Procedure<R, O, N> {
    return new Procedure(name, builtIn);
  }

  builtIn<N extends string>(name: N): BuiltIn<N> {
    return new BuiltIn(name);
  }

  star(): Star<any> {
    return new Star<any>();
  }

  invokeAsTable<T extends RowObject = any>(
    func: XObjectName | Func<string>,
    args: XExpression<Scalar>[]
  ): XRowset<T> {
    return Func.ensure(func).invokeAsTable<T>(...args);
  }

  invokeAsScalar<T extends Scalar = any>(
    func: XObjectName | Func<string>,
    args: XExpression<Scalar>[]
  ): ScalarFuncInvoke<T> {
    return Func.ensure(func).invokeAsScalar<T>(...args);
  }

  makeInvoke(
    type: 'table' | 'scalar',
    name: XObjectName,
    builtIn = false
  ): any {
    if (type === 'table') {
      return (
        ...args: (Star | BuiltIn | XExpression | undefined)[]
      ): XRowset<RowObject> => {
        return this.invokeAsTable(
          this.func(name, builtIn),
          args.map(p => (p === undefined ? null : p)) as XExpression[]
        );
      };
    }
    if (type === 'scalar') {
      return (
        ...args: (Star | BuiltIn | XExpression<Scalar> | undefined)[]
      ): Expression => {
        return this.invokeAsScalar<Scalar>(
          this.func(name, builtIn),
          args.map(p => (p === undefined ? null : p)) as XExpression[]
        );
      };
    }
    throw new Error('invalid arg value of `type`');
  }

  makeExec(name: XObjectName, builtIn = false): any {
    return (
      ...args: (BuiltIn | XExpression | undefined)[]
    ): Execute<any, any> => {
      return this.execute(
        this.proc<Scalar, any, string>(name, builtIn),
        args.map(p => (p === undefined ? null : p)) as XExpression[]
      );
    };
  }

  //********************** statement **************************//

  /**
   * 赋值语句
   * @param left 左值
   * @param right 右值
   */
  set<T extends Scalar = any>(
    left: Assignable<T>,
    right: XExpression<T>
  ): Assignment<T> {
    return new Assignment(left, right);
  }
  /**
   * 变量声明
   * @param declares 变量列表
   */
  declare(
    ...members:
      | (Variant<any> | TableVariant<any>)[]
      | [(Variant<any> | TableVariant<any>)[]]
  ): Declare {
    if (members.length == 1 && Array.isArray(members[0])) {
      members = members[0];
    }
    return new Declare(members as (Variant<any> | TableVariant<any>)[]);
  }
  /**
   * WHEN 语句块
   * @param expr
   * @param value
   */
  when<T extends Scalar>(
    expr: XExpression<Scalar>,
    value: XExpression<T>
  ): When<T> {
    return new When(expr, value);
  }

  case<T extends Scalar>(expr?: XExpression): Case<T> {
    return new Case<T>(expr);
  }
  /**
   * With语句
   */
  with(
    ...rowsets:
      | WithSelect[]
      | [WithSelect[]]
      | XWithSelect[]
      | [XWithSelect[]]
      | [SelectAliasObject]
  ): With {
    return new With(...rowsets);
  }

  union<T extends RowObject = any>(...selects: Select<T>[]): Select<T> {
    selects.forEach((sel, index) => {
      if (index < selects.length - 1) sel.union(selects[index + 1]);
    });
    return selects[0];
  }
  unionAll<T extends RowObject = any>(...selects: Select<T>[]): Select<T> {
    selects.forEach((sel, index) => {
      if (index < selects.length - 1) sel.unionAll(selects[index + 1]);
    });
    return selects[0];
  }

  get createTable() {
    return CreateTableBuilder;
  }

  get alterTable() {
    return AlterTableBuilder;
  }

  get createView() {
    return CreateViewBuilder;
  }

  alterView<T extends RowObject = any, N extends string = string>(
    name: XObjectName<N>
  ): AlterView<T, N> {
    return new AlterView(name);
  }

  createIndex(name: string): CreateIndex {
    return new CreateIndex(name);
  }

  get createProcedure() {
    return CreateProcedureBuilder;
  }

  get alterProcedure() {
    return AlterProcedureBuilder;
  }

  get createFunction() {
    return CreateFunctionBuilder;
  }

  get alterFunction() {
    return AlterFunctionBuilder;
  }

  get dropProcedure() {
    return DropProcedureBuilder;
  }

  get dropTable() {
    return DropTableBuilder;
  }

  get dropView() {
    return DropViewBuilder;
  }

  get dropFunction() {
    return DropFunctionBuilder;
  }

  dropIndex<N extends string>(table: XObjectName, name: N): DropIndex<N> {
    return new DropIndex(table, name);
  }

  annotation(...text: string[]): Annotation {
    return new Annotation('BLOCK', text.join('\n'));
  }

  note(text: string): Annotation {
    return new Annotation('LINE', text);
  }

  /**
   * 插入至表,into的别名
   * @param table
   * @param fields
   */
  insert<T extends RowObject = any>(
    table: XTables<T>,
    fields?: ColumnsOf<T>[] | Field<Scalar, ColumnsOf<T>>[]
  ): Insert<T> {
    return new Insert(table, fields);
  }
  /**
   * 更新一个表格
   * @param table
   */
  update<T extends RowObject = any>(table: XTables<T>): Update<T> {
    return new Update(table);
  }
  /**
   * 删除一个表格
   * @param table 表格
   */
  delete<T extends RowObject = any>(table: XTables<T>): Delete<T> {
    return new Delete(table);
  }

  select(...args: any[]): any {
    // if (args.length === 0) {
    //   args = [SQL.star];
    // }
    return new Select(...args);
  }

  raw(sql: string): any {
    return new Raw(sql);
  }

  block(...args: Statement[] | [Statement[]]): Block {
    if (args.length === 1 && Array.isArray(args[0])) {
      args = args[0];
    }
    return new Block(args as Statement[]);
  }

  execute<R extends Scalar = any, O extends RowObject[] = []>(
    proc: XObjectName | Procedure<R, O, string>,
    params?: XExpression<Scalar>[]
    // | Parameter<JsConstant, string>[] | InputObject
  ): Execute<R, O> {
    return new Execute(proc, params);
  }

  get createDatabase() {
    return CreateDatabaseBuilder;
  }

  alterDatabase(name: string): AlterDatabase {
    return new AlterDatabase(name);
  }

  get dropDatabase() {
    return DropDatabaseBuilder;
  }

  get createSchema() {
    return CreateSchemaBuilder;
  }

  get dropSchema() {
    return DropSchemaBuilder;
  }

  get createSequence() {
    return CreateSequenceBuilder;
  }

  get dropSequence() {
    return DropSequenceBuilder;
  }

  input(name: string, typeOrValue: DbType | Scalar, value?: Scalar): Parameter {
    let type: DbType | undefined;
    if (isDbType(typeOrValue)) {
      type = typeOrValue;
    } else {
      value = typeOrValue;
    }
    return new Parameter(name, type, value, 'IN');
  }

  /**
   * output参数
   */
  output<T extends DbType, N extends string>(
    name: N,
    type: T,
    value?: ScalarFromDbType<T>
  ): Parameter<ExpandScalar<ScalarFromDbType<T>>, N> {
    return new Parameter(name, type, value, 'OUT');
  }

  /**
   * 创建一个输入输出参数
   */
  inoutput<T extends DbType, N extends string>(
    name: N,
    type: T,
    value?: ScalarFromDbType<T>
  ): Parameter<ExpandScalar<ScalarFromDbType<T>>, N> {
    return new Parameter(name, type, value, 'INOUT');
  }

  //***************************End 参数声明 ***************************//

  sequence<T extends Numeric>(name: XObjectName): Sequence<T> {
    return new Sequence(name);
  }

  count(expr: Star | XExpression<Scalar>): Expression<number> {
    return new VirtualExpression(this.count.name, [expr]);
  }

  avg<T extends Numeric>(expr: XExpression<T>): Expression<T> {
    return new VirtualExpression(this.avg.name, [expr]);
  }
  sum<T extends Numeric>(expr: XExpression<T>): Expression<T> {
    return new VirtualExpression(this.sum.name, [expr]);
  }

  max<
    T extends
      | string
      | number
      | bigint
      | boolean
      | Decimal
      | Date
      | Time
      | Uuid
      | null
  >(expr: Expression<T>): Expression<T> {
    return new VirtualExpression(this.max.name, [expr]);
  }

  min<
    T extends
      | string
      | number
      | bigint
      | boolean
      | Decimal
      | Date
      | Time
      | Uuid
      | null
  >(expr: Expression<T>): Expression<T> {
    return new VirtualExpression(this.min.name, [expr]);
  }

  /**
   * 获取标识列的最近插入值
   */
  identityValue(
    table: XExpression<string>,
    column: XExpression<string>
  ): Expression<number | bigint> {
    return new VirtualExpression(this.identityValue.name, [table, column]);
  }
  /**
   * 转换数据类型
   */
  convert<T extends DbType>(
    expr: XExpression,
    toType: T
  ): Expression<ScalarFromDbType<T>> {
    return new VirtualExpression(this.convert.name, [expr, toType]);
  }
  /**
   * 获取当前日期及时间
   */
  now(): Expression<Date> {
    return new VirtualExpression(this.now.name, []);
  }
  /**
   * 获取当前UTC时间
   */
  utcNow(): Expression<Date> {
    return new VirtualExpression(this.utcNow.name, []);
  }
  /**
   * 切换时区
   */
  switchTimezone(
    date: XExpression<Date>,
    offset: XExpression<string>
  ): Expression<Date> {
    return new VirtualExpression(this.switchTimezone.name, [date, offset]);
  }
  /**
   * 格式化日期函数
   */
  formatDate(date: XExpression<Date>, format: string): Expression<string> {
    return new VirtualExpression(this.formatDate.name, [date, format]);
  }
  /**
   * 获取日期中的年份
   * @param date
   * @returns
   */
  yearOf(date: XExpression<Date>): Expression<number> {
    return new VirtualExpression(this.yearOf.name, [date]);
  }
  /**
   * 获取日期中的月份
   * @param date
   * @returns
   */
  monthOf(date: XExpression<Date>): Expression<number> {
    return new VirtualExpression(this.monthOf.name, [date]);
  }
  /**
   * 获取日期中的日
   * @param date
   * @returns
   */
  dayOf(date: XExpression<Date>): Expression<number> {
    return new VirtualExpression(this.dayOf.name, [date]);
  }
  /**
   * 计算两个日期之间的天数，小数
   * @param start
   * @param end
   * @returns
   */
  daysBetween(
    start: XExpression<Date>,
    end: XExpression<Date>
  ): Expression<number> {
    return new VirtualExpression(this.daysBetween.name, [start, end]);
  }
  /**
   * 计算两个日期之间的月数，小数
   * @param start
   * @param end
   * @returns
   */
  monthsBetween(
    start: XExpression<Date>,
    end: XExpression<Date>
  ): Expression<number> {
    return new VirtualExpression(this.monthsBetween.name, [start, end]);
  }
  /**
   * 计算两个日期之间的天数，小数
   * @param start
   * @param end
   * @returns
   */
  yearsBetween(
    start: XExpression<Date>,
    end: XExpression<Date>
  ): Expression<number> {
    return new VirtualExpression(this.yearsBetween.name, [start, end]);
  }
  /**
   * 计算两个日期之间的天数，小数
   * @param start
   * @param end
   * @returns
   */
  hoursBetween(
    start: XExpression<Date>,
    end: XExpression<Date>
  ): Expression<number> {
    return new VirtualExpression(this.hoursBetween.name, [start, end]);
  }
  /**
   * 计算两个日期之间的天数，小数
   * @param start
   * @param end
   * @returns
   */
  minutesBetween(
    start: XExpression<Date>,
    end: XExpression<Date>
  ): Expression<number> {
    return new VirtualExpression(this.minutesBetween.name, [start, end]);
  }
  /**
   * 计算两个日期之间的天数，小数
   * @param start
   * @param end
   * @returns
   */
  secondsBetween(
    start: XExpression<Date>,
    end: XExpression<Date>
  ): Expression<number> {
    return new VirtualExpression(this.secondsBetween.name, [start, end]);
  }
  addDays(
    date: XExpression<Date>,
    days: XExpression<Numeric>
  ): Expression<Date> {
    return new VirtualExpression(this.addDays.name, [date, days]);
  }
  addMonths(
    date: XExpression<Date>,
    months: XExpression<Numeric>
  ): Expression<Date> {
    return new VirtualExpression(this.addMonths.name, [date, months]);
  }
  addYears(
    date: XExpression<Date>,
    years: XExpression<Numeric>
  ): Expression<Date> {
    return new VirtualExpression(this.addYears.name, [date, years]);
  }
  addHours(
    date: XExpression<Date>,
    hours: XExpression<Numeric>
  ): Expression<Date> {
    return new VirtualExpression(this.addHours.name, [date, hours]);
  }
  addMinutes(
    date: XExpression<Date>,
    minutes: XExpression<Numeric>
  ): Expression<Date> {
    return new VirtualExpression(this.addMinutes.name, [date, minutes]);
  }
  addSeconds(
    date: XExpression<Date>,
    seconds: XExpression<Numeric>
  ): Expression<Date> {
    return new VirtualExpression(this.addSeconds.name, [date, seconds]);
  }
  /**
   * 获取字符串长度
   * @param str
   * @returns
   */
  strlen(str: XExpression<string>): Expression<number> {
    return new VirtualExpression(this.strlen.name, [str]);
  }
  /**
   * 截取字符串
   * @param str
   * @param start
   * @param length
   * @returns
   */
  substr(
    str: XExpression<string>,
    start: XExpression<Numeric>,
    length: XExpression<Numeric>
  ): Expression<string> {
    return new VirtualExpression(this.substr.name, [start, length]);
  }
  /**
   * 替换字符串
   * @param str 需要被替换的字符串
   * @param search 查找字符串
   * @param to 替换成字符串
   * @param global 是否全局替换，默认为false
   * @returns
   */
  replace(
    str: XExpression<string>,
    search: XExpression<string>,
    to: XExpression<string>
  ): Expression<string> {
    return new VirtualExpression(this.replace.name, [str, search, to]);
  }
  /**
   * 删除字符串两侧空格
   * @param str
   * @returns
   */
  trim(str: XExpression<string>): Expression<string> {
    return new VirtualExpression(this.trim.name, [str]);
  }
  /**
   * 删除字符串右侧空格
   * @param str
   * @returns
   */
  trimEnd(str: XExpression<string>): Expression<string> {
    return new VirtualExpression(this.trimEnd.name, [str]);
  }
  /**
   * 转换成大写字母
   * @param str
   * @returns
   */
  upper(str: XExpression<string>): Expression<string> {
    return new VirtualExpression(this.upper.name, [str]);
  }
  /**
   * 转换成小写字母
   * @param str
   * @returns
   */
  lower(str: XExpression<string>): Expression<string> {
    return new VirtualExpression(this.lower.name, [str]);
  }
  /**
   * 查找一个
   * @param str
   * @param search
   * @returns
   */
  strpos(
    str: XExpression<string>,
    search: XExpression<string>,
    startAt?: XExpression<number>
  ): Expression<number> {
    return new VirtualExpression(this.strpos.name, [str, search, startAt]);
  }
  // /**
  //  * 获取一个字符的ascii码
  //  * @param str 字符编码
  //  * @returns
  //  */
  // ascii(str: XExpression<string>): Expression<number> {
  //   return new VirtualExpression(SQL.ascii.name, [str]);
  // },
  // asciiChar(code: XExpression<number>): Expression<string> {
  //   return new VirtualExpression(SQL.asciiChar.name, [code]);
  // },
  // unicode(str: XExpression<string>): Expression<number> {
  //   return new VirtualExpression(SQL.unicode.name, [str]);
  // },
  // unicodeChar(code: XExpression<number>): Expression<string> {
  //   return new VirtualExpression(SQL.unicodeChar.name, [code]);
  // },
  nvl<T extends Scalar>(
    value: XExpression<T>,
    defaultValue: XExpression<T>
  ): Expression<T> {
    return new VirtualExpression(this.nvl.name, [value, defaultValue]);
  }
  abs<T extends Numeric>(value: XExpression<T>): Expression<T> {
    return new VirtualExpression(this.abs.name, [value]);
  }
  exp<T extends Numeric>(value: XExpression<T>): Expression<T> {
    return new VirtualExpression(this.exp.name, [value]);
  }

  ceil<T extends Numeric>(value: XExpression<T>): Expression<T> {
    return new VirtualExpression(this.ceil.name, [value]);
  }
  floor<T extends Numeric>(value: XExpression<T>): Expression<T> {
    return new VirtualExpression(this.floor.name, [value]);
  }
  ln<T extends Numeric>(value: XExpression<T>): Expression<T> {
    return new VirtualExpression(this.ln.name, [value]);
  }
  log<T extends Numeric>(value: XExpression<T>): Expression<T> {
    return new VirtualExpression(this.log.name, [value]);
  }

  pi(): Expression<number> {
    return new VirtualExpression(this.pi.name, []);
  }
  power<T extends Numeric>(
    a: XExpression<T>,
    b: XExpression<Numeric>
  ): Expression<T> {
    return new VirtualExpression(this.power.name, [a, b]);
  }
  radians<T extends Numeric>(value: XExpression<T>): Expression<T> {
    return new VirtualExpression(this.radians.name, [value]);
  }
  degrees<T extends Numeric>(value: XExpression<T>): Expression<T> {
    return new VirtualExpression(this.degrees.name, [value]);
  }
  random(): Expression<Float> {
    return new VirtualExpression(this.random.name, []);
  }
  round<T extends Numeric>(
    value: XExpression<T>,
    s?: XExpression<Numeric>
  ): Expression<T> {
    return new VirtualExpression(this.round.name, [value, s]);
  }
  sign<T extends Numeric>(value: XExpression<T>): Expression<T> {
    return new VirtualExpression(this.sign.name, [value]);
  }
  sqrt(value: XExpression<Numeric>): Expression<Float> {
    return new VirtualExpression(this.sqrt.name, [value]);
  }
  cos(value: XExpression<Numeric>): Expression<Float> {
    return new VirtualExpression(this.cos.name, [value]);
  }
  sin(value: XExpression<Numeric>): Expression<Float> {
    return new VirtualExpression(this.sin.name, [value]);
  }
  tan(value: XExpression<Numeric>): Expression<Float> {
    return new VirtualExpression(this.tan.name, [value]);
  }
  acos(value: XExpression<Numeric>): Expression<Float> {
    return new VirtualExpression(this.acos.name, [value]);
  }
  asin(value: XExpression<Numeric>): Expression<Float> {
    return new VirtualExpression(this.asin.name, [value]);
  }
  atan(value: XExpression<Numeric>): Expression<Float> {
    return new VirtualExpression(this.atan.name, [value]);
  }
  atan2(value: XExpression<Numeric>): Expression<Float> {
    return new VirtualExpression(this.atan2.name, [value]);
  }
  cot(value: XExpression<Numeric>): Expression<Float> {
    return new VirtualExpression(this.cot.name, [value]);
  }
  currentDatabase(): Expression<string> {
    return new VirtualExpression(this.currentDatabase.name, []);
  }

  defaultSchema(): Expression<string> {
    return new VirtualExpression(this.defaultSchema.name, []);
  }
  nextValue<T extends Numeric>(
    sequenceName: XExpression<string>
  ): Expression<T> {
    return new VirtualExpression(this.nextValue.name, [sequenceName]);
  }
  existsTable(table: XExpression<string>): Condition {
    return new VirtualCondition(this.existsTable.name, [table]);
  }
  existsDatabase(database: string): Condition {
    return new VirtualCondition(this.existsDatabase.name, [database]);
  }
  existsView(name: XExpression<string>): Condition {
    return new VirtualCondition(this.existsView.name, [name]);
  }
  existsFunction(name: XExpression<string>): Condition {
    return new VirtualCondition(this.existsFunction.name, [name]);
  }
  existsProcedure(name: XExpression<string>): Condition {
    return new VirtualCondition(this.existsProcedure.name, [name]);
  }
  existsSequence(name: XExpression<string>): Condition {
    return new VirtualCondition(this.existsSequence.name, [name]);
  }
}

export const SQL: SqlBuilder = new XSqlBuilder();

// export const SQL: SqlBuilder = {
//   get type(): typeof DbType {
//     return DbType;
//   },

//   // use(database: string): Use {
//   //   return new Use(database);
//   // },

//   if(condition: Condition): If {
//     return new If(condition);
//   },

//   while(condition: Condition): While {
//     return new While(condition);
//   },

//   break(): Break {
//     return new Break();
//   },

//   continue(): Continue {
//     return new Continue();
//   },

//   return(value?: XExpression): Return {
//     return new Return(value);
//   },

//   /**
//    * 创建一个SQL文档
//    * @param statements 文档代码
//    */
//   doc(...statements: Statement[] | [Statement[]]): Document {
//     const lines = Array.isArray(statements[0])
//       ? statements[0]
//       : (statements as Statement[]);
//     return new Document(lines);
//   },

//   group(value: Condition | XExpression): any {
//     if (Condition.isCondition(value)) {
//       return new GroupCondition(value);
//     }
//     return new GroupExpression(value as XExpression);
//   },

//   /**
//    * 负号运算符 -
//    */
//   neg(expr: XExpression<number>): Expression<ExpandScalar<number>> {
//     return new UnaryOperation(UNARY_OPERATION_OPERATOR.NEG, expr);
//   },

//   /**
//    * 字符串连接运算
//    */
//   concat(
//     ...strs: [
//       XExpression<string>,
//       XExpression<string>,
//       ...XExpression<string>[]
//     ]
//   ): Expression<string> {
//     let exp = strs[0];
//     for (let i = 1; i < strs.length; i++) {
//       exp = new BinaryOperation(BINARY_OPERATION_OPERATOR.CONCAT, exp, strs[i]);
//     }
//     return exp as Expression<string>;
//   },

//   /**
//    * 算术运算 +
//    * @param left 左值
//    * @param right 右值
//    * @returns 返回算术运算表达式
//    */
//   add<T extends Numeric>(
//     left: XExpression<T>,
//     right: XExpression<T>
//   ): Expression<T> {
//     return new BinaryOperation(BINARY_OPERATION_OPERATOR.ADD, left, right);
//   },

//   /**
//    * 算术运算 -
//    * @param left 左值
//    * @param right 右值
//    * @returns 返回算术运算表达式
//    */
//   sub<T extends Numeric>(
//     left: XExpression<T>,
//     right: XExpression<T>
//   ): Expression<T> {
//     return new BinaryOperation(BINARY_OPERATION_OPERATOR.SUB, left, right);
//   },

//   /**
//    * 算术运算 *
//    * @param left 左值
//    * @param right 右值
//    * @returns 返回算术运算表达式
//    */
//   mul<T extends Numeric>(
//     left: XExpression<T>,
//     right: XExpression<T>
//   ): Expression<T> {
//     return new BinaryOperation(BINARY_OPERATION_OPERATOR.MUL, left, right);
//   },

//   /**
//    * 算术运算 /
//    * @param left 左值
//    * @param right 右值
//    * @returns 返回算术运算表达式
//    */
//   div<T extends Numeric>(
//     left: XExpression<T>,
//     right: XExpression<T>
//   ): Expression<T> {
//     return new BinaryOperation(BINARY_OPERATION_OPERATOR.DIV, left, right);
//   },

//   /**
//    * 算术运算 %
//    * @param left 左值
//    * @param right 右值
//    * @returns 返回算术运算表达式
//    */
//   mod<T extends Numeric>(
//     left: XExpression<T>,
//     right: XExpression<T>
//   ): Expression<T> {
//     return new BinaryOperation(BINARY_OPERATION_OPERATOR.MOD, left, right);
//   },
//   /*
//    * 位算术运算 ^
//    * @param left 左值
//    * @param right 右值
//    * @returns 返回算术运算表达式
//    */
//   xor<T extends Integer>(
//     left: XExpression<T>,
//     right: XExpression<T>
//   ): Expression<T> {
//     return new BinaryOperation(BINARY_OPERATION_OPERATOR.XOR, left, right);
//   },

//   /**
//    * 位算术运算 << 仅支持整型
//    * @param left 左值
//    * @param right 右值
//    * @returns 返回算术运算表达式
//    */
//   shl<T extends Integer>(
//     left: XExpression<T>,
//     right: XExpression<T>
//   ): Expression<T> {
//     return new BinaryOperation(BINARY_OPERATION_OPERATOR.SHL, left, right);
//   },

//   /**
//    * 位算术运算 >>
//    * @param left 左值
//    * @param right 右值
//    * @returns 返回算术运算表达式
//    */
//   shr<T extends Integer>(
//     left: XExpression<T>,
//     right: XExpression<T>
//   ): Expression<T> {
//     return new BinaryOperation(BINARY_OPERATION_OPERATOR.SHR, left, right);
//   },

//   literal<T extends Scalar>(
//     value: T,
//     dbType?: DbTypeFromScalar<T>
//   ): Literal<ExpandScalar<T>> {
//     return new Literal(value as any, dbType);
//   },

//   var<T extends Scalar, N extends string = string>(
//     name: N,
//     type: DbTypeFromScalar<T>
//   ): Variant<ExpandScalar<T>, N> {
//     return new Variant(name, type) as any;
//   },
//   /**
//    * 创建一个字段
//    */
//   field<T extends Scalar, N extends string>(
//     name: N,
//     rowset?: XRowsets
//   ): Field<T, N> {
//     return new Field(name, rowset);
//   },

//   and(
//     ...args:
//       | Condition[]
//       | [Condition[]]
//       | [XExpression<Integer>, XExpression<Integer>]
//   ): any {
//     if (Array.isArray(args[0])) {
//       args = args[0];
//     }

//     if (Expression.isExpression(args[0]) || isBaseScalar(args[0])) {
//       return new BinaryOperation(
//         BINARY_OPERATION_OPERATOR.AND,
//         args[0],
//         args[1] as XExpression<Integer>
//       );
//     }

//     return ConditionBuilder.and(args as Condition[]);
//   },

//   rownum(orderBy: Sort[], partitionBy?: Field): Expression<number> {
//     return new VirtualExpression(SQL.rownum.name, [orderBy, partitionBy]);
//   },

//   or(
//     ...args:
//       | Condition[]
//       | [Condition[]]
//       | [XExpression<Integer>, XExpression<Integer>]
//   ): any {
//     if (Array.isArray(args[0])) {
//       args = args[0];
//     }
//     if (Expression.isExpression(args[0]) || isBaseScalar(args[0])) {
//       return new BinaryOperation(
//         BINARY_OPERATION_OPERATOR.OR,
//         args[0],
//         args[1] as XExpression<Integer>
//       );
//     }
//     return ConditionBuilder.or(args as Condition[]);
//   },

//   not(arg: Condition | XExpression<Integer>): any {
//     if (Expression.isExpression(arg) || isBaseScalar(arg)) {
//       return new UnaryOperation(UNARY_OPERATION_OPERATOR.NOT, arg);
//     }
//     return new UnaryLogicCondition(LOGIC_OPERATOR.NOT, arg);
//   },

//   /**
//    * 比较运算 IS NULL
//    * @returns 返回比较运算符
//    * @param expr 表达式
//    */
//   isNull(expr: XExpression<Scalar>): Condition {
//     return new UnaryCompareCondition(UNARY_COMPARE_OPERATOR.IS_NULL, expr);
//   },
//   /**
//    * 比较运算 IS NOT NULL
//    * @param expr 表达式
//    * @returns 返回比较运算符
//    */
//   isNotNull(expr: XExpression<Scalar>): Condition {
//     return new UnaryCompareCondition(UNARY_COMPARE_OPERATOR.IS_NOT_NULL, expr);
//   },

//   eq<T extends Scalar>(left: XExpression<T>, right: XExpression<T>): Condition {
//     return new BinaryCompareCondition(BINARY_COMPARE_OPERATOR.EQ, left, right);
//   },

//   /**
//    * 比较运算 <>
//    * @param left 左值
//    * @param right 右值
//    * @returns 返回比较运算对比条件
//    */
//   neq<T extends Scalar>(
//     left: XExpression<T>,
//     right: XExpression<T>
//   ): Condition {
//     return new BinaryCompareCondition(BINARY_COMPARE_OPERATOR.NEQ, left, right);
//   },

//   lt<T extends Scalar>(left: XExpression<T>, right: XExpression<T>): Condition {
//     return new BinaryCompareCondition(BINARY_COMPARE_OPERATOR.LT, left, right);
//   },
//   /**
//    * 比较运算 <=
//    * @param left 左值
//    * @param right 右值
//    * @returns 返回比较运算对比条件
//    */
//   lte<T extends Scalar>(
//     left: XExpression<T>,
//     right: XExpression<T>
//   ): Condition {
//     return new BinaryCompareCondition(BINARY_COMPARE_OPERATOR.LTE, left, right);
//   },
//   /**
//    * 比较运算 >
//    * @param left 左值
//    * @param right 右值
//    * @returns 返回比较运算对比条件
//    */
//   gt<T extends Scalar>(left: XExpression<T>, right: XExpression<T>): Condition {
//     return new BinaryCompareCondition(BINARY_COMPARE_OPERATOR.GT, left, right);
//   },
//   /**
//    * 比较运算 >=
//    * @param left 左值
//    * @param right 右值
//    * @returns 返回比较运算对比条件
//    */
//   gte<T extends Scalar>(
//     left: XExpression<T>,
//     right: XExpression<T>
//   ): Condition {
//     return new BinaryCompareCondition(BINARY_COMPARE_OPERATOR.GTE, left, right);
//   },
//   /**
//    * 比较运算 LIKE
//    * @param left 左值
//    * @param right 右值
//    * @returns 返回比较运算对比条件
//    */
//   like(left: XExpression<string>, right: XExpression<string>): Condition {
//     return new BinaryCompareCondition(
//       BINARY_COMPARE_OPERATOR.LIKE,
//       left,
//       right
//     );
//   },
//   /**
//    * 比较运算 NOT LIKE
//    * @param left 左值
//    * @param right 右值
//    * @returns 返回比较运算对比条件
//    */
//   notLike(left: XExpression<string>, right: XExpression<string>): Condition {
//     return new BinaryCompareCondition(
//       BINARY_COMPARE_OPERATOR.NOT_LIKE,
//       left,
//       right
//     );
//   },

//   in<T extends Scalar>(
//     left: XExpression<T>,
//     values: XExpression<T>[] | Select<any>
//   ): Condition {
//     return new BinaryCompareCondition(
//       BINARY_COMPARE_OPERATOR.IN,
//       left,
//       Select.isSelect(values)
//         ? values.asValue()
//         : values.map(v => Expression.ensureExpression(v))
//     );
//   },

//   /**
//    * 比较运算 NOT IN
//    * @param left 左值
//    * @param values 要比较的值列表
//    * @returns 返回比较运算对比条件
//    */
//   notIn<T extends Scalar>(
//     left: XExpression<T>,
//     values: XExpression<T>[]
//   ): Condition {
//     return new BinaryCompareCondition(
//       BINARY_COMPARE_OPERATOR.NOT_IN,
//       left,
//       values.map(v => Expression.ensureExpression(v))
//     );
//   },
//   /**
//    * 判断是否存在
//    * @param select 查询语句
//    */
//   exists(select: Select<any>): Condition {
//     return new ExistsCondition(select);
//   },

//   table(
//     name: XObjectName,
//     builtInOrMembers:
//       | boolean
//       | TableVariantMember[]
//       | ((builder: TableVariantBuilder) => TableVariantMember[]) = false
//   ): any {
//     if (
//       typeof builtInOrMembers === 'function' ||
//       Array.isArray(builtInOrMembers)
//     ) {
//       return createTableVariant(
//         name as string,
//         builtInOrMembers as
//           | ((builder: TableVariantBuilder) => TableVariantMember[])
//           | TableVariantMember[]
//       );
//     }
//     return Table.create(name, builtInOrMembers as boolean);
//   },
//   /**
//    * 声明一个函数
//    */
//   func(name: XObjectName, builtIn = false): Func {
//     return new Func(name, builtIn);
//   },
//   /**
//    * 创建一个可供调用的存储过程函数
//    */
//   proc<
//     R extends Scalar = number,
//     O extends RowObject[] = never,
//     N extends string = string
//   >(name: XObjectName<N>, builtIn = false): Procedure<R, O, N> {
//     return new Procedure(name, builtIn);
//   },

//   builtIn<N extends string>(name: N): BuiltIn<N> {
//     return new BuiltIn(name);
//   },

//   star(): Star<any> {
//     return new Star<any>();
//   },

//   invokeAsTable<T extends RowObject = any>(
//     func: XObjectName | Func<string>,
//     args: XExpression<Scalar>[]
//   ): XRowset<T> {
//     return Func.ensure(func).invokeAsTable<T>(...args);
//   },

//   invokeAsScalar<T extends Scalar = any>(
//     func: XObjectName | Func<string>,
//     args: XExpression<Scalar>[]
//   ): ScalarFuncInvoke<T> {
//     return Func.ensure(func).invokeAsScalar<T>(...args);
//   },

//   makeInvoke(
//     type: 'table' | 'scalar',
//     name: XObjectName,
//     builtIn = false
//   ): any {
//     if (type === 'table') {
//       return function (
//         ...args: (Star | BuiltIn | XExpression | undefined)[]
//       ): XRowset<RowObject> {
//         return SQL.invokeAsTable(
//           SQL.func(name, builtIn),
//           args.map(p => (p === undefined ? null : p))
//         );
//       };
//     }
//     if (type === 'scalar') {
//       return function (
//         ...args: (Star | BuiltIn | XExpression<Scalar> | undefined)[]
//       ): Expression {
//         return SQL.invokeAsScalar<Scalar>(
//           SQL.func(name, builtIn),
//           args.map(p => (p === undefined ? null : p))
//         );
//       };
//     }
//     throw new Error('invalid arg value of `type`');
//   },

//   makeExec(name: XObjectName, builtIn = false): any {
//     return function (
//       ...args: (BuiltIn | XExpression | undefined)[]
//     ): Execute<any, any> {
//       return SQL.execute(
//         SQL.proc<Scalar, any, string>(name, builtIn),
//         args.map(p => (p === undefined ? null : p))
//       );
//     };
//   },

//   //********************** statement **************************//

//   /**
//    * 赋值语句
//    * @param left 左值
//    * @param right 右值
//    */
//   set<T extends Scalar = any>(
//     left: Assignable<T>,
//     right: XExpression<T>
//   ): Assignment<T> {
//     return new Assignment(left, right);
//   },
//   /**
//    * 变量声明
//    * @param declares 变量列表
//    */
//   declare(
//     ...members:
//       | (Variant<any> | TableVariant<any>)[]
//       | [(Variant<any> | TableVariant<any>)[]]
//   ): Declare {
//     if (members.length == 1 && Array.isArray(members[0])) {
//       members = members[0];
//     }
//     return new Declare(members as (Variant<any> | TableVariant<any>)[]);
//   },
//   /**
//    * WHEN 语句块
//    * @param expr
//    * @param value
//    */
//   when<T extends Scalar>(
//     expr: XExpression<Scalar>,
//     value: XExpression<T>
//   ): When<T> {
//     return new When(expr, value);
//   },

//   case<T extends Scalar>(expr?: XExpression): Case<T> {
//     return new Case<T>(expr);
//   },
//   /**
//    * With语句
//    */
//   with(
//     ...rowsets:
//       | WithSelect[]
//       | [WithSelect[]]
//       | XWithSelect[]
//       | [XWithSelect[]]
//       | [SelectAliasObject]
//   ): With {
//     return new With(...rowsets);
//   },

//   union<T extends RowObject = any>(...selects: Select<T>[]): Select<T> {
//     selects.forEach((sel, index) => {
//       if (index < selects.length - 1) sel.union(selects[index + 1]);
//     });
//     return selects[0];
//   },
//   unionAll<T extends RowObject = any>(...selects: Select<T>[]): Select<T> {
//     selects.forEach((sel, index) => {
//       if (index < selects.length - 1) sel.unionAll(selects[index + 1]);
//     });
//     return selects[0];
//   },

//   get createTable() {
//     return CreateTableBuilder;
//   },

//   get alterTable() {
//     return AlterTableBuilder;
//   },

//   get createView() {
//     return CreateViewBuilder;
//   },

//   alterView<T extends RowObject = any, N extends string = string>(
//     name: XObjectName<N>
//   ): AlterView<T, N> {
//     return new AlterView(name);
//   },

//   createIndex(name: string): CreateIndex {
//     return new CreateIndex(name);
//   },

//   get createProcedure() {
//     return CreateProcedureBuilder;
//   },

//   get alterProcedure() {
//     return AlterProcedureBuilder;
//   },

//   get createFunction() {
//     return CreateFunctionBuilder;
//   },

//   get alterFunction() {
//     return AlterFunctionBuilder;
//   },

//   get dropProcedure() {
//     return DropProcedureBuilder;
//   },

//   get dropTable() {
//     return DropTableBuilder;
//   },

//   get dropView() {
//     return DropViewBuilder;
//   },

//   get dropFunction() {
//     return DropFunctionBuilder;
//   },

//   dropIndex<N extends string>(table: XObjectName, name: N): DropIndex<N> {
//     return new DropIndex(table, name);
//   },

//   annotation(...text: string[]): Annotation {
//     return new Annotation('BLOCK', text.join('\n'));
//   },

//   note(text: string): Annotation {
//     return new Annotation('LINE', text);
//   },

//   /**
//    * 插入至表,into的别名
//    * @param table
//    * @param fields
//    */
//   insert<T extends RowObject = any>(
//     table: XTables<T>,
//     fields?: ColumnsOf<T>[] | Field<Scalar, ColumnsOf<T>>[]
//   ): Insert<T> {
//     return new Insert(table, fields);
//   },
//   /**
//    * 更新一个表格
//    * @param table
//    */
//   update<T extends RowObject = any>(table: XTables<T>): Update<T> {
//     return new Update(table);
//   },
//   /**
//    * 删除一个表格
//    * @param table 表格
//    */
//   delete<T extends RowObject = any>(table: XTables<T>): Delete<T> {
//     return new Delete(table);
//   },

//   select(...args: any[]): any {
//     // if (args.length === 0) {
//     //   args = [SQL.star];
//     // }
//     return new Select(...args);
//   },

//   raw(sql: string): any {
//     return new Raw(sql);
//   },

//   block(...args: Statement[] | [Statement[]]): Block {
//     if (args.length === 1 && Array.isArray(args[0])) {
//       args = args[0];
//     }
//     return new Block(args as Statement[]);
//   },

//   execute<R extends Scalar = any, O extends RowObject[] = []>(
//     proc: XObjectName | Procedure<R, O, string>,
//     params?: XExpression<Scalar>[]
//     // | Parameter<JsConstant, string>[] | InputObject
//   ): Execute<R, O> {
//     return new Execute(proc, params);
//   },

//   get createDatabase() {
//     return CreateDatabaseBuilder;
//   },
//   alterDatabase(name: string): AlterDatabase {
//     return new AlterDatabase(name);
//   },
//   get dropDatabase() {
//     return DropDatabaseBuilder;
//   },
//   get createSchema() {
//     return CreateSchemaBuilder;
//   },
//   get dropSchema() {
//     return DropSchemaBuilder;
//   },
//   get createSequence() {
//     return CreateSequenceBuilder;
//   },
//   get dropSequence() {
//     return DropSequenceBuilder;
//   },

//   input(name: string, typeOrValue: DbType | Scalar, value?: Scalar): Parameter {
//     let type: DbType | undefined;
//     if (isDbType(typeOrValue)) {
//       type = typeOrValue;
//     } else {
//       value = typeOrValue;
//     }
//     return new Parameter(name, type, value, 'IN');
//   },

//   /**
//    * output参数
//    */
//   output<T extends DbType, N extends string>(
//     name: N,
//     type: T,
//     value?: ScalarFromDbType<T>
//   ): Parameter<ExpandScalar<ScalarFromDbType<T>>, N> {
//     return new Parameter(name, type, value, 'OUT');
//   },

//   /**
//    * 创建一个输入输出参数
//    */
//   inoutput<T extends DbType, N extends string>(
//     name: N,
//     type: T,
//     value?: ScalarFromDbType<T>
//   ): Parameter<ExpandScalar<ScalarFromDbType<T>>, N> {
//     return new Parameter(name, type, value, 'INOUT');
//   },

//   //***************************End 参数声明 ***************************//

//   sequence<T extends Numeric>(name: XObjectName): Sequence<T> {
//     return new Sequence(name);
//   },
//   count(expr: Star | XExpression<Scalar>): Expression<number> {
//     return new VirtualExpression(SQL.count.name, [expr]);
//   },
//   avg<T extends Numeric>(expr: XExpression<T>): Expression<T> {
//     return new VirtualExpression(SQL.avg.name, [expr]);
//   },
//   sum<T extends Numeric>(expr: XExpression<T>): Expression<T> {
//     return new VirtualExpression(SQL.sum.name, [expr]);
//   },

//   max<
//     T extends
//       | string
//       | number
//       | bigint
//       | boolean
//       | Decimal
//       | Date
//       | Time
//       | Uuid
//       | null
//   >(expr: Expression<T>): Expression<T> {
//     return new VirtualExpression(SQL.max.name, [expr]);
//   },

//   min<
//     T extends
//       | string
//       | number
//       | bigint
//       | boolean
//       | Decimal
//       | Date
//       | Time
//       | Uuid
//       | null
//   >(expr: Expression<T>): Expression<T> {
//     return new VirtualExpression(SQL.min.name, [expr]);
//   },
//   /**
//    * 获取标识列的最近插入值
//    */
//   identityValue(
//     table: XExpression<string>,
//     column: XExpression<string>
//   ): Expression<number | bigint> {
//     return new VirtualExpression(SQL.identityValue.name, [table, column]);
//   },
//   /**
//    * 转换数据类型
//    */
//   convert<T extends DbType>(
//     expr: XExpression,
//     toType: T
//   ): Expression<ScalarFromDbType<T>> {
//     return new VirtualExpression(SQL.convert.name, [expr, toType]);
//   },
//   /**
//    * 获取当前日期及时间
//    */
//   now(): Expression<Date> {
//     return new VirtualExpression(SQL.now.name, []);
//   },
//   /**
//    * 获取当前UTC时间
//    */
//   utcNow(): Expression<Date> {
//     return new VirtualExpression(SQL.utcNow.name, []);
//   },
//   /**
//    * 切换时区
//    */
//   switchTimezone(
//     date: XExpression<Date>,
//     offset: XExpression<string>
//   ): Expression<Date> {
//     return new VirtualExpression(SQL.switchTimezone.name, [date, offset]);
//   },
//   /**
//    * 格式化日期函数
//    */
//   formatDate(date: XExpression<Date>, format: string): Expression<string> {
//     return new VirtualExpression(SQL.formatDate.name, [date, format]);
//   },
//   /**
//    * 获取日期中的年份
//    * @param date
//    * @returns
//    */
//   yearOf(date: XExpression<Date>): Expression<number> {
//     return new VirtualExpression(SQL.yearOf.name, [date]);
//   },
//   /**
//    * 获取日期中的月份
//    * @param date
//    * @returns
//    */
//   monthOf(date: XExpression<Date>): Expression<number> {
//     return new VirtualExpression(SQL.monthOf.name, [date]);
//   },
//   /**
//    * 获取日期中的日
//    * @param date
//    * @returns
//    */
//   dayOf(date: XExpression<Date>): Expression<number> {
//     return new VirtualExpression(SQL.dayOf.name, [date]);
//   },
//   /**
//    * 计算两个日期之间的天数，小数
//    * @param start
//    * @param end
//    * @returns
//    */
//   daysBetween(
//     start: XExpression<Date>,
//     end: XExpression<Date>
//   ): Expression<number> {
//     return new VirtualExpression(SQL.daysBetween.name, [start, end]);
//   },
//   /**
//    * 计算两个日期之间的月数，小数
//    * @param start
//    * @param end
//    * @returns
//    */
//   monthsBetween(
//     start: XExpression<Date>,
//     end: XExpression<Date>
//   ): Expression<number> {
//     return new VirtualExpression(SQL.monthsBetween.name, [start, end]);
//   },
//   /**
//    * 计算两个日期之间的天数，小数
//    * @param start
//    * @param end
//    * @returns
//    */
//   yearsBetween(
//     start: XExpression<Date>,
//     end: XExpression<Date>
//   ): Expression<number> {
//     return new VirtualExpression(SQL.yearsBetween.name, [start, end]);
//   },
//   /**
//    * 计算两个日期之间的天数，小数
//    * @param start
//    * @param end
//    * @returns
//    */
//   hoursBetween(
//     start: XExpression<Date>,
//     end: XExpression<Date>
//   ): Expression<number> {
//     return new VirtualExpression(SQL.hoursBetween.name, [start, end]);
//   },
//   /**
//    * 计算两个日期之间的天数，小数
//    * @param start
//    * @param end
//    * @returns
//    */
//   minutesBetween(
//     start: XExpression<Date>,
//     end: XExpression<Date>
//   ): Expression<number> {
//     return new VirtualExpression(SQL.minutesBetween.name, [start, end]);
//   },
//   /**
//    * 计算两个日期之间的天数，小数
//    * @param start
//    * @param end
//    * @returns
//    */
//   secondsBetween(
//     start: XExpression<Date>,
//     end: XExpression<Date>
//   ): Expression<number> {
//     return new VirtualExpression(SQL.secondsBetween.name, [start, end]);
//   },
//   addDays(
//     date: XExpression<Date>,
//     days: XExpression<Numeric>
//   ): Expression<Date> {
//     return new VirtualExpression(SQL.addDays.name, [date, days]);
//   },
//   addMonths(
//     date: XExpression<Date>,
//     months: XExpression<Numeric>
//   ): Expression<Date> {
//     return new VirtualExpression(SQL.addMonths.name, [date, months]);
//   },
//   addYears(
//     date: XExpression<Date>,
//     years: XExpression<Numeric>
//   ): Expression<Date> {
//     return new VirtualExpression(SQL.addYears.name, [date, years]);
//   },
//   addHours(
//     date: XExpression<Date>,
//     hours: XExpression<Numeric>
//   ): Expression<Date> {
//     return new VirtualExpression(SQL.addHours.name, [date, hours]);
//   },
//   addMinutes(
//     date: XExpression<Date>,
//     minutes: XExpression<Numeric>
//   ): Expression<Date> {
//     return new VirtualExpression(SQL.addMinutes.name, [date, minutes]);
//   },
//   addSeconds(
//     date: XExpression<Date>,
//     seconds: XExpression<Numeric>
//   ): Expression<Date> {
//     return new VirtualExpression(SQL.addSeconds.name, [date, seconds]);
//   },
//   /**
//    * 获取字符串长度
//    * @param str
//    * @returns
//    */
//   strlen(str: XExpression<string>): Expression<number> {
//     return new VirtualExpression(SQL.strlen.name, [str]);
//   },
//   /**
//    * 截取字符串
//    * @param str
//    * @param start
//    * @param length
//    * @returns
//    */
//   substr(
//     str: XExpression<string>,
//     start: XExpression<Numeric>,
//     length: XExpression<Numeric>
//   ): Expression<string> {
//     return new VirtualExpression(SQL.substr.name, [start, length]);
//   },
//   /**
//    * 替换字符串
//    * @param str 需要被替换的字符串
//    * @param search 查找字符串
//    * @param to 替换成字符串
//    * @param global 是否全局替换，默认为false
//    * @returns
//    */
//   replace(
//     str: XExpression<string>,
//     search: XExpression<string>,
//     to: XExpression<string>
//   ): Expression<string> {
//     return new VirtualExpression(SQL.replace.name, [str, search, to]);
//   },
//   /**
//    * 删除字符串两侧空格
//    * @param str
//    * @returns
//    */
//   trim(str: XExpression<string>): Expression<string> {
//     return new VirtualExpression(SQL.trim.name, [str]);
//   },
//   /**
//    * 删除字符串右侧空格
//    * @param str
//    * @returns
//    */
//   trimEnd(str: XExpression<string>): Expression<string> {
//     return new VirtualExpression(SQL.trimEnd.name, [str]);
//   },
//   /**
//    * 转换成大写字母
//    * @param str
//    * @returns
//    */
//   upper(str: XExpression<string>): Expression<string> {
//     return new VirtualExpression(SQL.upper.name, [str]);
//   },
//   /**
//    * 转换成小写字母
//    * @param str
//    * @returns
//    */
//   lower(str: XExpression<string>): Expression<string> {
//     return new VirtualExpression(SQL.lower.name, [str]);
//   },
//   /**
//    * 查找一个
//    * @param str
//    * @param search
//    * @returns
//    */
//   strpos(
//     str: XExpression<string>,
//     search: XExpression<string>,
//     startAt?: XExpression<number>
//   ): Expression<number> {
//     return new VirtualExpression(SQL.strpos.name, [str, search, startAt]);
//   },
//   // /**
//   //  * 获取一个字符的ascii码
//   //  * @param str 字符编码
//   //  * @returns
//   //  */
//   // ascii(str: XExpression<string>): Expression<number> {
//   //   return new VirtualExpression(SQL.ascii.name, [str]);
//   // },
//   // asciiChar(code: XExpression<number>): Expression<string> {
//   //   return new VirtualExpression(SQL.asciiChar.name, [code]);
//   // },
//   // unicode(str: XExpression<string>): Expression<number> {
//   //   return new VirtualExpression(SQL.unicode.name, [str]);
//   // },
//   // unicodeChar(code: XExpression<number>): Expression<string> {
//   //   return new VirtualExpression(SQL.unicodeChar.name, [code]);
//   // },
//   nvl<T extends Scalar>(
//     value: XExpression<T>,
//     defaultValue: XExpression<T>
//   ): Expression<T> {
//     return new VirtualExpression(SQL.nvl.name, [value, defaultValue]);
//   },
//   abs<T extends Numeric>(value: XExpression<T>): Expression<T> {
//     return new VirtualExpression(SQL.abs.name, [value]);
//   },
//   exp<T extends Numeric>(value: XExpression<T>): Expression<T> {
//     return new VirtualExpression(SQL.exp.name, [value]);
//   },

//   ceil<T extends Numeric>(value: XExpression<T>): Expression<T> {
//     return new VirtualExpression(SQL.ceil.name, [value]);
//   },
//   floor<T extends Numeric>(value: XExpression<T>): Expression<T> {
//     return new VirtualExpression(SQL.floor.name, [value]);
//   },
//   ln<T extends Numeric>(value: XExpression<T>): Expression<T> {
//     return new VirtualExpression(SQL.ln.name, [value]);
//   },
//   log<T extends Numeric>(value: XExpression<T>): Expression<T> {
//     return new VirtualExpression(SQL.log.name, [value]);
//   },

//   pi(): Expression<number> {
//     return new VirtualExpression(SQL.pi.name, []);
//   },
//   power<T extends Numeric>(
//     a: XExpression<T>,
//     b: XExpression<Numeric>
//   ): Expression<T> {
//     return new VirtualExpression(SQL.power.name, [a, b]);
//   },
//   radians<T extends Numeric>(value: XExpression<T>): Expression<T> {
//     return new VirtualExpression(SQL.radians.name, [value]);
//   },
//   degrees<T extends Numeric>(value: XExpression<T>): Expression<T> {
//     return new VirtualExpression(SQL.degrees.name, [value]);
//   },
//   random(): Expression<Float> {
//     return new VirtualExpression(SQL.random.name, []);
//   },
//   round<T extends Numeric>(
//     value: XExpression<T>,
//     s?: XExpression<Numeric>
//   ): Expression<T> {
//     return new VirtualExpression(SQL.round.name, [value, s]);
//   },
//   sign<T extends Numeric>(value: XExpression<T>): Expression<T> {
//     return new VirtualExpression(SQL.sign.name, [value]);
//   },
//   sqrt(value: XExpression<Numeric>): Expression<Float> {
//     return new VirtualExpression(SQL.sqrt.name, [value]);
//   },
//   cos(value: XExpression<Numeric>): Expression<Float> {
//     return new VirtualExpression(SQL.cos.name, [value]);
//   },
//   sin(value: XExpression<Numeric>): Expression<Float> {
//     return new VirtualExpression(SQL.sin.name, [value]);
//   },
//   tan(value: XExpression<Numeric>): Expression<Float> {
//     return new VirtualExpression(SQL.tan.name, [value]);
//   },
//   acos(value: XExpression<Numeric>): Expression<Float> {
//     return new VirtualExpression(SQL.acos.name, [value]);
//   },
//   asin(value: XExpression<Numeric>): Expression<Float> {
//     return new VirtualExpression(SQL.asin.name, [value]);
//   },
//   atan(value: XExpression<Numeric>): Expression<Float> {
//     return new VirtualExpression(SQL.atan.name, [value]);
//   },
//   atan2(value: XExpression<Numeric>): Expression<Float> {
//     return new VirtualExpression(SQL.atan2.name, [value]);
//   },
//   cot(value: XExpression<Numeric>): Expression<Float> {
//     return new VirtualExpression(SQL.cot.name, [value]);
//   },
//   currentDatabase(): Expression<string> {
//     return new VirtualExpression(SQL.currentDatabase.name, []);
//   },

//   defaultSchema(): Expression<string> {
//     return new VirtualExpression(SQL.defaultSchema.name, []);
//   },
//   nextValue<T extends Numeric>(
//     sequenceName: XExpression<string>
//   ): Expression<T> {
//     return new VirtualExpression(SQL.nextValue.name, [sequenceName]);
//   },
//   existsTable(table: XExpression<string>): Condition {
//     return new VirtualCondition(SQL.existsTable.name, [table]);
//   },
//   existsDatabase(database: string): Condition {
//     return new VirtualCondition(SQL.existsDatabase.name, [database]);
//   },
//   existsView(name: XExpression<string>): Condition {
//     return new VirtualCondition(SQL.existsView.name, [name]);
//   },
//   existsFunction(name: XExpression<string>): Condition {
//     return new VirtualCondition(SQL.existsFunction.name, [name]);
//   },
//   existsProcedure(name: XExpression<string>): Condition {
//     return new VirtualCondition(SQL.existsProcedure.name, [name]);
//   },
//   existsSequence(name: XExpression<string>): Condition {
//     return new VirtualCondition(SQL.existsSequence.name, [name]);
//   },
// };
